package main

import (
	"html/template"
	"net/http"
)

func index(w http.ResponseWriter, r *http.Request) {
	//定义模版
	//解析模版
	t, err := template.ParseFiles("./index.tmpl")
	if err != nil {
		panic(err)
	}
	msg := "index页面"
	//渲染模版
	t.Execute(w, msg)
}
func home(w http.ResponseWriter, r *http.Request) {
	//定义模版
	//解析模版
	t, err := template.ParseFiles("./index.tmpl")
	if err != nil {
		panic(err)
	}
	msg := "home页面"
	//渲染模版
	t.Execute(w, msg)
}

func index2(w http.ResponseWriter, r *http.Request) {
	//定义模版
	//解析模版
	t, err := template.ParseFiles("./template/base.tmpl", "./template/index.tmpl")
	if err != nil {
		panic(err)
	}
	msg := "张三"
	//渲染模版
	t.ExecuteTemplate(w, "index.tmpl", msg)
}
func home2(w http.ResponseWriter, r *http.Request) {
	//定义模版
	//解析模版
	t, err := template.ParseFiles("./template/base.tmpl", "./template/home.tmpl")
	if err != nil {
		panic(err)
	}
	msg := "李四"
	//渲染模版
	t.ExecuteTemplate(w, "home.tmpl", msg)
}

func main() {
	http.HandleFunc("/index", index)
	http.HandleFunc("/home", home)
	http.HandleFunc("/index2", index2)
	http.HandleFunc("/home2", home2)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		panic(err)
	}
}
